package com.smart.manager.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ExceptionTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.work.entities.body.RPigOrder;
import com.smart.manager.work.entities.models.PigOrder;
import com.smart.manager.work.repository.PigOrderRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * 工单资料
 *
 * @author leo
 * @date 2023/09/20
 */
@Api(tags = "工单管理")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "work/pig/order")
@RequiredArgsConstructor
public class PigOrderApi {

    private final PigOrderRepository repository;

    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询工单")
    @SaCheckPermission("work:pig:order:pageList")
    public AjaxResult<Page<PigOrder>> pageList(@Validated RPigOrder.SearchBody body) {
        Page<PigOrder> result = repository.pageList(body);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "添加工单")
    @PostMapping("add")
    @SaCheckPermission("work:pig:order:add")
    public AjaxResult<String> add(@RequestBody @Validated RPigOrder.EditBody data) {
        repository.add(data);
        return AjaxResult.success();
    }


    @ApiOperation(value = "修改工单")
    @PutMapping("update/{id}")
    @SaCheckPermission("work:pig:order:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RPigOrder.EditBody data) {
        repository.update(id, data);
        return AjaxResult.success();
    }

    @ApiOperation(value = "入卷工单")
    @PutMapping("submit/{id}")
    @SaCheckPermission("work:pig:order:submit")
    public AjaxResult<String> submit(@PathVariable Long id, @RequestBody @Validated RPigOrder.enterTheCircleBody body) {
        repository.submit(id, body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "分配工单")
    @PutMapping("assign/{id}/{code}")
    @SaCheckPermission("work:pig:order:assign")
    public AjaxResult<String> assign(@PathVariable Long id, @PathVariable String code) {
        System.out.println(id + "==" + code);
        repository.assign(id, code);
        return AjaxResult.success();
    }


    @ApiOperation(value = "删除工单")
    @DeleteMapping("delete")
    @SaCheckPermission("work:pig:order:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ExceptionTips.PK_EMPTY) List<Long> ids) {
        int i = repository.delByIds(ids);
        return AjaxResult.delSuccess(i);
    }

}
